package com.zijie.hosptal.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijie.hosptal.exception.DsException;
import com.zijie.hosptal.exception.RepeatDeptException;
import com.zijie.hosptal.mapper.DoctorSchedulesMapper;
import com.zijie.hosptal.mapper.ReserveMapper;
import com.zijie.hosptal.pojo.po.Doctor;
import com.zijie.hosptal.pojo.po.DoctorSchedules;
import com.zijie.hosptal.pojo.vo.DoctorSchedulesVO;
import com.zijie.hosptal.service.DoctorSchedulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class DoctorSchedulesServiceImpl implements DoctorSchedulesService {
    private DoctorSchedulesMapper doctorSchedulesMapper;

    private ReserveMapper reserveMapper;

    @Autowired
    public void setReserveMapper(ReserveMapper reserveMapper) {
        this.reserveMapper = reserveMapper;
    }

    @Autowired
    public void setDoctorSchedulesMapper(DoctorSchedulesMapper doctorSchedulesMapper) {
        this.doctorSchedulesMapper = doctorSchedulesMapper;
    }

    @Override
    public Page<DoctorSchedulesVO> list(Page<DoctorSchedulesVO> schedulePage, DoctorSchedules doctorSchedules) {

        Page<DoctorSchedulesVO> p = doctorSchedulesMapper.findList(schedulePage, doctorSchedules);
        return p;
    }

    //增加
    @Override
    public boolean save(DoctorSchedules doctorSchedules) {
        DoctorSchedules doctorSchedules1 = doctorSchedulesMapper.selectDoctorSchedule(doctorSchedules);
        if (doctorSchedules1 != null) {
            throw new RepeatDeptException("该科室在您预约的日期已存在活动，请改天预约");
        }
        DoctorSchedules doctorSchedules2 = doctorSchedulesMapper.selectIsDoctor(doctorSchedules);
        if (doctorSchedules2 != null) {
            throw new RepeatDeptException("该医生在您预约的日期已存在活动，请更换医生");
        }
        LocalDateTime startTime = doctorSchedules.getStartTime();
        if (startTime.isBefore(LocalDateTime.now())) {
            throw new DsException("活动时间不能早于当前时间");
        }
        return doctorSchedulesMapper.insert(doctorSchedules) > 0;
    }

    @Override
    public boolean removeById(Integer id) {
        Integer id1 = reserveMapper.selectByDSId(id);
        if (id1 > 0) {
            throw new DsException("该活动有预约，请先取消预约");
        }
        return doctorSchedulesMapper.deleteById(id) > 0;
    }

    @Transactional
    @Override
    public boolean updateById(DoctorSchedules doctorSchedules) {
        LocalDateTime startTime = doctorSchedules.getStartTime();
        if (!startTime.toLocalDate().isAfter(LocalDateTime.now().toLocalDate())) {
            throw new DsException("修改的活动时间只可以在今天之后");
        }


       // 检查同一科室在同一天是否已存在活动（排除当前正在更新的记录）
DoctorSchedules doctorSchedules1 = doctorSchedulesMapper.selectDoctorSchedule(doctorSchedules);
if (doctorSchedules1 != null && !Objects.equals(doctorSchedules.getId(), doctorSchedules1.getId())) {
    throw new RepeatDeptException("该科室在您预约的日期已存在活动，请改天预约");
}

// 检查同一医生在同一天是否已存在活动（排除当前正在更新的记录）
DoctorSchedules doctorSchedules2 = doctorSchedulesMapper.selectIsDoctor(doctorSchedules);
if (doctorSchedules2 != null && !Objects.equals(doctorSchedules.getId(), doctorSchedules2.getId())) {
    throw new RepeatDeptException("该医生在您预约的日期已存在活动，请更换医生");
}

//        String status = doctorSchedules.getStatus();
        //查看当前预约的人数
        Integer nowCount = reserveMapper.selectByDSId(doctorSchedules.getId());
        //获取当前活动修改的人数----增加人
        Integer totalSlots = doctorSchedules.getTotalSlots();
        if (nowCount > totalSlots) {
            throw new DsException("当前活动预约人数已经达到" + nowCount + "人，不能减少为" + totalSlots + "人");
        }
        if (Objects.equals(doctorSchedules.getStatus(), "2")) {
            doctorSchedules.setStatus("1");
            doctorSchedulesMapper.updateStatusById(doctorSchedules);
        }
        //如果人数减少了 要检查状态是否满员
        if (nowCount.equals(totalSlots) && !Objects.equals(doctorSchedules.getStatus(), "2")) {
            doctorSchedules.setStatus("2");
            doctorSchedulesMapper.updateStatusById(doctorSchedules);
        }

        return doctorSchedulesMapper.updateById(doctorSchedules) > 0;
    }

    @Override
    public List<DoctorSchedules> getAll() {
        List<DoctorSchedules> list = doctorSchedulesMapper.getAll();
        for (DoctorSchedules doctorSchedules : list) {
            //为每个活动添加目前的预约人数
            doctorSchedules.setReserveCount(reserveMapper.selectByDSId(doctorSchedules.getId()));
        }
        return list;
    }

    @Override
    public Integer getReservedCount(Integer id) {
        return reserveMapper.selectByDSId(id);
    }
}
